﻿using System;
using System.Web.UI.WebControls;
using System.Linq;
using System.Collections.Generic;

namespace ECommerce.Administration.Controls
{
	public partial class ProductTagList : AdminBaseUsercontrol
	{
		private int _ProductID;
		private Service.IProductTagService _ProductTagService;
		private Service.IProductService _ProductService;

		public int ProductID
		{
			get
			{
				if(ViewState["ProductID"] != null)
				{
					_ProductID = (int)ViewState["ProductID"];

					return _ProductID;
				}

				_ProductID = CommonHelper.QueryStringInt("productID");
				return _ProductID;
			}
			set
			{
				ViewState["ProductID"] = value;
				_ProductID = value;
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();

			SetTheProcuctTag();
		}

		protected void GridTags_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.TagList.NameHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.TagList.ProductCountHeader");
			}
		}

		private void BindData()
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (_ProductTagService == null)
			{
				_ProductTagService = new Service.ProductTagService();
			}
			
			var languageID = 0;

			if(SiteContext.Current.WorkingLanguage != null)
			{
				languageID = SiteContext.Current.WorkingLanguage.LanguageId;
				var data = _ProductTagService.GetProductTagByLanguage(languageID);

				GridTags.DataSource = data;
				GridTags.DataBind();
			}
		}

		private void SetTheProcuctTag()
		{
			if(ProductID > 0)
			{
				if(GridTags.Rows.Count > 0)
				{
					if (_ProductService == null)
					{
						_ProductService = new Service.ProductService();
					}

					var tags = _ProductService.GetTheProductTags(ProductID);

					if(tags != null)
					{
						var tagIds = (from t in tags
									  select t.ProductTagID);

						foreach(GridViewRow row in GridTags.Rows)
						{
							var cbProductTag = row.FindControl("cbProductTag") as CheckBox;
							var hfProductTagId = row.FindControl("hfProductTagId") as HiddenField;

							int currentTagID = int.Parse(hfProductTagId.Value);

							cbProductTag.Checked = tagIds.Contains(currentTagID);
						}
					}
				}
			}
		}

		public void DeleteSelectedTags()
		{
			if (_ProductTagService == null)
			{
				_ProductTagService = new Service.ProductTagService();
			}

			foreach(System.Web.UI.WebControls.GridViewRow rowItem in GridTags.Rows)
			{
				var chBox = rowItem.FindControl("cbProductTag") as CheckBox;
				var hfTagID = rowItem.FindControl("hfProductTagId") as HiddenField;

				bool isChecked = chBox.Checked;
				int tagID = int.Parse(hfTagID.Value);

				if(isChecked)
				{
					_ProductTagService.DeleteProductTag(tagID);
				}
			}

			BindData();
		}

		private IEnumerable<int> GetTheProduct_Tag_Mapping()
		{
			if (_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			return (from tagid in _ProductService.GetProductTag_Product_MappingByProduct(ProductID)
					select tagid.ProductTagID);
		}

		public void SaveTheProduct_Tag_Mapping()
		{
			//List<int> lstTagIDs = GetTheProduct_Tag_Mapping();
			foreach(GridViewRow row in GridTags.Rows)
			{
				var cbProductTag = row.FindControl("cbProductTag") as CheckBox;
				var hfProductTagId = row.FindControl("hfProductTagId") as HiddenField;

				if (_ProductService == null)
				{
					_ProductService = new Service.ProductService();
				}

				int currentTagID = int.Parse(hfProductTagId.Value);

				if(cbProductTag.Checked)
				{
					if (_ProductService.IsExistingTheProductAndTagMapping(ProductID, currentTagID) == 0)
					{
						Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping = new Data.Entity.ProductTag_Product_Mapping();

						theProductTag_Product_Mapping.ProductID = ProductID;
						theProductTag_Product_Mapping.ProductTagID = currentTagID;

						_ProductService.SaveProductTag_Product_Mapping(theProductTag_Product_Mapping);

						//do product tag count +1
						var productTagService = new Service.ProductTagService();
						productTagService.PlusOrMinus1ProductCount(currentTagID, 1);
					}
				}
				else
				{
					if (_ProductService.IsExistingTheProductAndTagMapping(ProductID, currentTagID) == 1)
					{
						Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping = new Data.Entity.ProductTag_Product_Mapping();

						theProductTag_Product_Mapping.ProductID = ProductID;
						theProductTag_Product_Mapping.ProductTagID = currentTagID;

						_ProductService.DeleteProductTag_Product_Mapping(theProductTag_Product_Mapping);

						//do product tag count -1
						var productTagService = new Service.ProductTagService();
						productTagService.PlusOrMinus1ProductCount(currentTagID, -1);
					}
				}
			}
		}
	}
}